package com.zy.service;

import com.zy.dao.FriendMapper;
import com.zy.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class FriendService
{

    @Autowired
    private FriendMapper friendMapper;

    /**
     * 通过好友名称和用户id查看是否已经添加
     * @param friendName 好友名称
     * @param userId 用户id
     */
    public int findFriend(String friendName, int userId, int status)
    {
        return friendMapper.findFriend(friendName, userId, status);
    }

    /**
     * 通过好友名称和用户id添加申请
     * @param friendName 好友名称
     * @param userId 用户id
     */
    public void addFriend(String friendName, int userId)
    {
        int friendId = friendMapper.findFriendByFriendName(friendName);//通过姓名找到id
        friendMapper.addFriend(friendId,userId,0);//添加一条申请记录,状态为0
    }

    /**
     * 通过用户id查询好友列表
     * @param userId
     * @return
     */
    public List<User> findFriendListByUserId(int userId,String friendName)
    {
        List<User> list = null;
        if("".equals(friendName))
        {
            list = friendMapper.findFriendListByUserIdNoFriendName(userId,1);
        }
        else
        {
            list = friendMapper.findFriendListByUserId(userId,friendName,1);
        }
        log.debug("好友列表:{}",list);
        return list;
    }

    /**
     * 查询申请列表
     * @param userId
     * @return
     */
    public List<User> findApplysListByUserId(int userId)
    {
        return friendMapper.findFriendListByUserIdNoFriendName(userId,0);
    }

    /**
     * 修改好友申请状态,并将自己加入好友的列表中
     * @param userId
     * @param userId
     * @param status
     */
    public void agreeFriendStatus(int userId, int friendId, int status)
    {
        friendMapper.updateFriendStatus(userId,friendId,status);
        friendMapper.addFriend(friendId,userId,1);
    }

    /**
     * 拒绝申请
     * @param userId
     * @param friendId
     */
    public void deleteFriendApply(int userId, int friendId)
    {
        friendMapper.deleteFriendApply(userId,friendId);
    }

}
